package com.biz.user.db;

import com.alibaba.fastjson.*;
import com.google.common.base.*;
import com.biz.handler.*;
import com.biz.user.dao.*;
import com.biz.user.entity.po.*;
import lombok.extern.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import javax.annotation.*;
import java.util.*;
import java.util.stream.*;

/**
 * @Description
 *      系统用户数据归档
 * @Author liuxi
 * @Date 2024/5/17
 **/
@Slf4j
@Component
public class SysUserBackUpDataHandler extends AbstractBackUpDataHandler<SysUser, SysUserBackUpDataRule> {

    // @Value("")
    private String sysUserBackupRuleStr;

    /**
     * 归档停止flag，配合nacos
     */
    private Boolean stopFlag;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private SysUserBackDao sysUserBackDao;

    @Override
    public SysUserBackUpDataRule getRule() {
        SysUserBackUpDataRule backUpRule = JSON.parseObject(sysUserBackupRuleStr, SysUserBackUpDataRule.class);
        Preconditions.checkNotNull(backUpRule, "归档规则不能为空！");
        Preconditions.checkNotNull(backUpRule.getQuerySize(), "查询数量不能为空！");
        Preconditions.checkNotNull(backUpRule.getBeginId(), "beginId不能为空！");
        Long endId = backUpRule.getBeginId() + backUpRule.getQuerySize();
        backUpRule.setEndId(endId);
        return backUpRule;
    }

    @Override
    public Boolean needStop() {
        return stopFlag;
    }

    @Override
    public List<SysUser> queryData(SysUserBackUpDataRule rule) {
        SysUser sysUser = new SysUser();
        // 加入规则查询方式
        List<SysUser> sysUserList = sysUserDao.queryAll(sysUser);
        return sysUserList;
    }

    @Override
    public SysUserBackUpDataRule changeOffSet(SysUserBackUpDataRule rule) {
        rule.setBeginId(rule.getEndId());
        Long endId = rule.getBeginId() + rule.getQuerySize();
        rule.setEndId(endId);
        return rule;
    }

    @Override
    public void insertData(List<SysUser> dataList) {
        sysUserBackDao.insertBatch(dataList);
    }

    @Override
    public void deleteData(List<SysUser> dataList) {
        List<Long> ids = dataList.stream().map(SysUser::getId).collect(Collectors.toList());
        sysUserDao.batchDelete(ids);
    }
}
